import { MockMethod } from "vite-plugin-mock";
// @ts-ignore
import Mock from "mockjs";
// @ts-ignore
import { RequestStatus } from "@/types";

const users = Array.from({ length: 100 }, (_, index) => {
	return Mock.mock({
		id: "@id",
		username: "@name",
		email: "@email",
		avatar: '@image("200x200")',
		role: '@pick(["admin", "user", "guest"])',
		createTime: "@datetime",
		lastLoginTime: "@datetime",
	});
});

export default [
	{
		url: "/api/login",
		method: "post",
		response: ({ body }) => {
			const { username, password } = body;

			if (username === "admin" && password === "123456") {
				return {
					code: RequestStatus.SUCCESS,
					data: {
						token: Mock.Random.guid(),
						userInfo: {
							id: "1",
							username: "admin",
							email: "admin@example.com",
							avatar: Mock.Random.image("200x200"),
							role: "admin",
							createTime: Mock.Random.datetime(),
							lastLoginTime: Mock.Random.datetime(),
						},
					},
				};
			}

			return {
				code: "1001",
				message: "用户名或密码错误",
			};
		},
	},
	{
		url: "/api/register",
		method: "post",
		response: ({ body }) => {
			const { username, password, email } = body;

			if (username && password && email) {
				return {
					code: RequestStatus.SUCCESS,
					data: {
						message: "注册成功",
					},
				};
			}

			return {
				code: "1002",
				message: "注册失败，请检查输入信息",
			};
		},
	},
	{
		url: "/api/forgot-password",
		method: "post",
		response: ({ body }) => {
			const { email } = body;

			if (email) {
				return {
					code: RequestStatus.SUCCESS,
					data: {
						message: "重置密码链接已发送到您的邮箱",
					},
				};
			}

			return {
				code: "1003",
				message: "邮箱不存在",
			};
		},
	},
	{
		url: "/api/users",
		method: "get",
		response: ({ query }) => {
			const { page = 1, pageSize = 10 } = query;
			const start = (page - 1) * pageSize;
			const end = start + pageSize;

			return {
				code: RequestStatus.SUCCESS,
				data: {
					total: users.length,
					list: users.slice(start, end),
				},
			};
		},
	},
] as MockMethod[];
